package com.wzz.common.calendar.views;

import java.util.Calendar;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;

public class Cell {
  // private static final String TAG = "Cell";
  protected Rect bound = null; // 格子方框

  protected int dayOfMonth = 1; // from 1 to 31
  protected int month = 1;// from 1 to 12
  protected int year = 1900;

  protected Paint paint = new Paint(Paint.SUBPIXEL_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
  protected int dx, dy;// 文字位置。

  public boolean isThisMonth = true;// 当前格子是否为日历显示的当前月份
  public boolean isFill = false;// 是否填充

  private int fillColor = 0;// 填充颜色

  public Cell(int dayOfMon, Rect rect, float textSize, boolean bold) {
    dayOfMonth = dayOfMon;
    bound = rect;

    paint.setTextSize(textSize);
    paint.setColor(Color.BLACK);
    if (bold) {// 设置是否为粗体
      paint.setFakeBoldText(true);
    }
    /* 设置文字位置 */
    dx = (int) paint.measureText(String.valueOf(dayOfMonth)) / 2;
    dy = (int) (-paint.ascent() + paint.descent()) / 2;
  }

  public Cell(int dayOfMon, Rect rect, float textSize) {
    this(dayOfMon, rect, textSize, false);
  }

  protected void draw(Canvas canvas) {
    Paint paint1 = new Paint();
    paint1.setColor(fillColor);
    RectF rect = new RectF(bound);
    if (isFill) {
      canvas.drawRect(rect, paint1);  
    }else{
      canvas.drawRect(bound, paint1);  
    }
    canvas.drawText(String.valueOf(dayOfMonth), bound.centerX() - dx, bound.centerY() + dy, paint);
    
  }

  /**
   * 获得当前格子的日期
   * 
   */
  public int getDayOfMonth() {
    return dayOfMonth;
  }

  /**
   * 碰撞检测……
   * 
   * @param x
   * @param y
   * @return
   */
  public boolean hitTest(int x, int y) {
    return bound.contains(x, y);
  }

  /**
   * 获得当前日期的边框
   * 
   * @return
   */
  public Rect getBound() {
    return bound;
  }

  public String toString() {
    return String.valueOf(dayOfMonth) + "(" + bound.toString() + ")";
  }

  public int getMonth() {
    return month;
  }

  public void setMonth(int month) {
    this.month = month;
  }

  public int getYear() {
    return year;
  }

  public void setYear(int year) {
    this.year = year;
  }

  public Calendar getDate() {
    Calendar calendar = Calendar.getInstance();
    calendar.set(year, month, dayOfMonth);
    return calendar;
  }

  public void setBackColor(boolean isFill,int color) {
    this.isFill = isFill;
    this.fillColor = color;
  }
}
